package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2023-02-01
 * Time:17:38
 */
//@Controller
//@ResponseBody
@RestController
public class Test {
    //    获取单个对象或者多个对象
    @RequestMapping("/aaa")
    public String test(String name, int age) {
        return name + ":" + age;
    }

    //    封装一个对象并获取
    @RequestMapping("/bbb")
    public String test(Student student) {
        return student.toString();
    }

    //form表单
    @RequestMapping("/ccc")
    public HashMap<String,Object> test1(String name,int age) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("age",age);
        return map;
    }
    //ajax
    @RequestMapping("/ddd")
    public HashMap<String,Object> test2(@RequestBody Student student) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("name",student.getName());
        map.put("age",student.getAge());
        return map;
    }
    //上传文件@RquestPart
    @RequestMapping("/eee")
    public String fileTest(@RequestPart("file")MultipartFile file) throws IOException {
        file.transferTo(new File("F:\\java se\\test.png"));
        return "测试成功";
    }
    //得到cookie
    @RequestMapping("/cookie")
    public String cookie(@CookieValue("cookieTest") String test) {
        return "cookie：" + test;
    }
    //得到session
    @RequestMapping("/sess2")
    public String sess2(@SessionAttribute(value = "username",required = false)
                                String username) {
        return "username："+username;
    }
    //请求头
    @RequestMapping("/getheader")
    public String getHead(@RequestHeader("User-agent") String useragent){
        return "User-agent:"+useragent;
    }
    //请求转发
    @RequestMapping("/test")
    public Object test(){
        return "forward:/getheader";
    }
    //请求重定向
    @RequestMapping("/test2")
    public Object test2(){
        return "redirect:/getheader.htm";
    }
}
